﻿using BLL;
using DevExpress.XtraGrid.Views.Grid;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 纺织贸易管理系统;
using 纺织贸易管理系统.其他窗体;
using 纺织贸易管理系统.设置窗体;
using 纺织贸易管理系统.选择窗体;

namespace 后整理管理系统.入库管理.配件入库
{
    public partial class 配件入库单 : Form
    {
        public FormUseful useful = FormUseful.新增;
        public List<ImportAndExportOfRawMaterials> danjumingxitables = new List<ImportAndExportOfRawMaterials>();
        public int rowindex;
        public DanjuTable danju = new DanjuTable();
        public 配件入库单()
        {
            InitializeComponent();
            CreateGrid.SetGridBaseattribute(gridView1);
        }

        private void 配件入库单_Load(object sender, EventArgs e)
        {
            if (useful == FormUseful.新增)
            {
                Init();
            }
            else
            {
                Edit();
                if (useful == FormUseful.查看)
                {
                    保存ToolStripMenuItem.Enabled = false;
                }
            }
        }
        private void Edit()
        {
            txtdanhao.Text = danju.dh;
            txtbeizhu.Text = danju.bz;
            txtkehu.Text = danju.ksmc;
            dateEdit1.Text = danju.rq.ToShortDateString();
            danjumingxitables = Connect.dbcontext.Queryable<ImportAndExportOfRawMaterials>().Where(x => x.DocNum == txtdanhao.Text).ToList();
            var length = danjumingxitables.Count;
            for (int i = 0; i < 30 - length; i++)
            {
                danjumingxitables.Add(new ImportAndExportOfRawMaterials() { DocNum = txtdanhao.Text });
            }
            gridControl1.DataSource = danjumingxitables;
        }
        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView1.DeleteRow(gridView1.FocusedRowHandle);
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                danjumingxitables[gridView1.FocusedRowHandle].Total = danjumingxitables[gridView1.FocusedRowHandle].Price * danjumingxitables[gridView1.FocusedRowHandle].InventoryQuantity;
                gridControl1.RefreshDataSource();
            }
            catch
            {
                MessageBox.Show("请输入数字。计算错误");
            }
        }
        private void 单据赋值()
        {
            danju.bz = txtbeizhu.Text;
            danju.dh = txtdanhao.Text;
            danju.djlx = DanjuLeiXing.配件采购单;
            danju.rq = dateEdit1.DateTime;
            danju.je = danjumingxitables.Sum(x => x.Total);
            danju.TotalMishu = danjumingxitables.Sum(x => x.InventoryQuantity);
            danju.own = Tools.CurrentUser.user.YHMC;
            danju.zhuangtai = "未审核";
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView1.CloseEditor();
            if (danjumingxitables.Where(x => x.ProductName != null).ToList().Count == 0)
            {
                ShowMessageBox.ShowErrorBox("没有检测到任何配件信息.请填写相应的原料后再保存！");
                return;
            }
            if (string.IsNullOrEmpty(txtkehu.Text))
            {
                Tools.ShowMessageBox.ShowErrorBox("供货商名称必须填写！保存失败");
                return;
            }
            单据赋值();
            try
            {
                Connect.dbcontext.BeginTran();
                if (useful == FormUseful.新增)
                {
                    配件采购BLL.保存单据(danju, danjumingxitables.Where(x => !string.IsNullOrEmpty(x.ProductName)).ToList());
                }
                else
                {
                    配件采购BLL.修改单据(danju, danjumingxitables.Where(x => !string.IsNullOrEmpty(x.ProductName)).ToList());
                }
                Connect.dbcontext.CommitTran();
                AlterDlg.Show("保存成功！");
                if (ShowMessageBox.ShowAskBox("是否继续新增操作？") == DialogResult.Yes)
                {
                    //清空所有控件
                    Init();
                }
                else
                {
                    MainForm.mainform.CloseTab();
                }
            }
            catch (Exception ex)
            {
                Connect.dbcontext.RollbackTran();
                ShowMessageBox.ShowErrorBox("保存的时候发送了错误" + ex.Message);

            }
        }
        private void Init()
        {
            foreach (Control c in this.groupControl1.Controls)
            {
                if (c is DevComponents.DotNetBar.Controls.TextBoxX || c is DevExpress.XtraEditors.ButtonEdit)
                {
                    c.Text = "";
                }
            }
            dateEdit1.DateTime = DateTime.Now;
            txtdanhao.Text = BianhaoBLL.CreatDanhao(FirstLetter.配件采购单, dateEdit1.DateTime, DanjuLeiXing.配件采购单);
            danjumingxitables.Clear();
            danjumingxitables = Connect.CreatConnect().Query<ImportAndExportOfRawMaterials>(x => x.DocNum == txtdanhao.Text);
            var length = danjumingxitables.Count;
            for (int i = 0; i < 30 - length; i++)
            {
                danjumingxitables.Add(new ImportAndExportOfRawMaterials() { DocNum = txtdanhao.Text });
            }
            gridControl1.DataSource = danjumingxitables;
            gridControl1.RefreshDataSource();
            useful = FormUseful.新增;
        }

        private void txtkehu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var fm = new 供货商选择() { linkman = new LXR() { ZJC = txtkehu.Text, MC = "" } };
                fm.ShowDialog();
                danju.ksbh = fm.linkman.BH;
                danju.ksmc = fm.linkman.MC;
                txtkehu.Text = danju.ksmc;
            }
        }

        private void txtkehu_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var fm = new 供货商选择() { linkman = new LXR() { ZJC = txtkehu.Text, MC = "" } };
            fm.ShowDialog();
            danju.ksbh = fm.linkman.BH;
            danju.ksmc = fm.linkman.MC;
            txtkehu.Text = danju.ksmc;
        }
    }
}
